# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/26 15:45
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : 文件操作.py
# @Software : PyCharm

# 打开/创建文件
def foo1():
    file = open('./test1.txt', mode='r', encoding='utf8')
    # file = open(r'D:\workspace\hz_t2\c10_文件操作\test1.txt')

    # 关闭文件
    file.close()


def foo2():
    print('---------------------')
    # with-open
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        str1 = file.read()  # 读取指定个数，在read()参数里加数字，特殊字符也算
        print(str1)


def foo3():
    print('---------------------')
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        str1 = file.readline()  # 读取第一行
        print(str1)


def foo4():
    # with-open
    print('---------------------')
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        list1 = file.readlines()  # 读取文件中的所有行,放入一个列表
        print(list1)


# 文件的写入
def foo5():
    with open('./test2.txt', 'w', encoding='utf8')as file:
        str1 = '犇焱燚龘'
        # write()：将字符串内容写入文件
        file.write(str1)

def foo6():
    with open('./test3.txt','w',encoding='utf8') as file:
        # writelines():将列表写入文件
        list1 = ['你\n妈\n的\n']
        file.writelines(list1)








if __name__ == '__main__':
    foo1()
    foo2()
    foo3()
    foo4()
    foo5()
    foo6()